package com.kun.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.kun.common.Constants;
import com.kun.common.Result;
import com.kun.entity.Role;
import com.kun.exception.MyServiceException;
import com.kun.service.RoleServiceImpl;
import com.kun.utils.FileUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleServiceImpl roleService;


    @GetMapping("/deleteRole")
    @ApiOperation("删除角色")
    @ResponseBody
    public Result deleteRole(@RequestParam("id")int id){
        roleService.deleteRole(id);
        return  Result.success();
    }

    @PostMapping("/addRole")
    @ApiOperation("新增角色")
    @ResponseBody
    public Result addRole(@RequestBody Role role){
        roleService.addRole(role);
        return  Result.success();
    }

    @PostMapping("/batchDeleteRole")
    @ApiOperation("批量删除角色")
    @ResponseBody
    public Result batchDeleteRole(@RequestBody List<Integer> ids){
        for (Integer id : ids) {
            this.deleteRole(id);
        }
        return  Result.success();
    }


    @GetMapping("/queryAllRoles")
    @ApiOperation("查询所有的角色")
    @ResponseBody
    public Result queryAllRoles(@ApiParam("当前页数") @RequestParam("pageIndex")int pageIndex,
                                @ApiParam("页面大小") @RequestParam("pageSize") int pageSize,
                                @ApiParam("角色名") @RequestParam(value = "roleName",required = false) String roleName){
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageIndex",pageIndex);
        map.put("pageSize",pageSize);
        map.put("roleName_like",roleName);

        List<Role> roles = roleService.selectAllRoles(map);
        return  Result.success(roles);
    }

    @GetMapping("/queryAllRoleFlags")
    @ApiOperation("查询所有的角色的唯一标识")
    @ResponseBody
    public Result queryAllRoleFlags(){
        HashMap<String, Object> map = new HashMap<>();
        List<Role> roles = roleService.selectAllRoles(map);
        ArrayList<String> result = new ArrayList<>();
        for (Role role : roles) {
            result.add(role.getRoleFlag());
        }
        return  Result.success(result);
    }

    @GetMapping("/queryAllRolesNum")
    @ApiOperation("查询文件总数")
    @ResponseBody
    public Result queryAllRolesNum(@ApiParam("文件名") @RequestParam(value = "roleName",required = false) String roleName){
        HashMap<String, Object> map = new HashMap<>();
        map.put("roleName_like",roleName);
        List<Role> roles = roleService.selectAllRoles(map);
        return  Result.success(roles.size());
    }










}
